import Axios from 'axios'
import qs from 'qs'
import config from '@/config/'

/**
 * post请求
 * @param action
 * @param param
 * @method post OR get
 * @returns {Promise<AxiosResponse<T>>}
 */
export const post = function (action, param) {
  let actions = action.split('/')
  let url = process.env.NODE_ENV === 'production' ? config.baseUrl.pro : config.baseUrl.dev
  let params = {
    pages: actions[0],
    doing: actions[1]
  }
  let send_data = Object.assign(params, param)
  return Axios.post(url, qs.stringify(send_data))
}

export const upload = function(action, data) {
  let actions = action.split('/')
  let url = process.env.NODE_ENV === 'production' ? config.baseUrl.pro : config.baseUrl.dev
  data.append('pages', actions[0])
  data.append('doing', actions[1])
  return Axios.post(
    url,
    data,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  )
}

/**
 * get 请求
 * @param action
 * @param param
 * @returns {Promise<AxiosResponse<T>>}
 */
export const get = function (action, param) {
  let actions = action.split('/')
  let url = process.env.NODE_ENV === 'production' ? config.baseUrl.pro : config.baseUrl.dev
  let params = {
    pages: actions[0],
    doing: actions[1]
  }
  let send_data = Object.assign(params, param)
  url += '&' + qs.stringify(send_data)
  return Axios.get(url)
}

export default { post, upload, get }
